const app = getApp()
Page({
  data: {
    probability: [
     {
        'score': '不及格',
        'text': '慧科欢迎你的到来～'
     },
     {
       'score': '60',
       'text': '英雄不问出处，更不论岁数'
     },
     {
       'score': '70',
       'text': '您已是慧科的前辈。'
     },
     {
       'score': '80',
       'text': '作为慧科的老人，慧科如你的孩子一般。'
     },
    ],
    comment:'',
    grade:'',
    starLength:0,
    rightNum:0,
    username:'',
    showMask: false,
    isIphoneX:false,
    buttomBanner: null,
    buttomBannerImg: null,
    buttomBannerID: null,
    buttomBannerPath: null,
    buttomBannerUrl: null

  },
  onShow(){
    let getAppInfo = app.globalData.userInfo,
        _this = this;
    console.log(app.globalData.userInfo,getAppInfo)
    this.setData({
      username: getAppInfo.nickName,
      isIphoneX: app.data.isIPhoneX
    }) 

    // wx.request({
    //   url: app.data.commonUrl + '/Server/GameCenter/buttomBanner?appflag=xcx12',
    //   method: 'GET',
    //   success: function (res) {
    //     _this.setData({
    //       buttomBanner: res.data.result.type,
    //       buttomBannerID: res.data.result.appid
    //     })
    //     if (res.data.result.type == 'image') {
    //       _this.setData({
    //         buttomBannerPath: res.data.result.path,
    //         buttomBannerImg: res.data.result.url,
    //         buttomBannerUrl: res.data.result.case_url
    //       })
    //     }
    //   }
    // })
      
  },
  openUrl() {
    if (this.data.buttomBannerID != '' && this.data.buttomBanner == 'image') {
      wx.navigateToMiniProgram({
        appId: this.data.buttomBannerID,
        path: this.data.buttomBannerPath,
        envVersion: 'release',
        fail(res) {
          wx.hideLoading();
        }
      })

    } else {
      wx.previewImage({
        current: this.data.buttomBannerUrl,
        urls: [this.data.buttomBannerUrl]
      })

    }
  },
  onLoad: function (options) { 
    if (options.rightNum==undefined){
      options.rightNum=0
    }
    console.log(options.rightNum,'options.rightNum')
    switch (options.rightNum) {
      case '6':
        this.setData({
          grade: '东北话四级',
          comment:'英雄不问出处，更不论岁数',
          starLength:1
        })
        break;
      case '7':
        this.setData({
          grade: '',
          comment: '您已是慧科的前辈。',
          starLength:2
        })
        break;
      case '8':
        this.setData({
          grade: '',
          comment: '作为慧科的老人，慧科如你的孩子一般。',
          starLength:3
        })
        break;
      
      default:
        this.setData({
          grade: '不及格',
          comment: '慧科欢迎你的到来～'
        })
    }
    this.setData({
      rightNum: options.rightNum
    })
    console.log(this.data.rightNum, this.data.grade, this.data.comment)
  },
  onShareAppMessage(res) {
  },
  saveFn() {
    wx.showLoading({
      title: '正在保存',
      mask: true
    });
    new Promise((resolve, reject) => {
      const ctx = wx.createCanvasContext('firstCanvas');
      //引入图片背景
      ctx.drawImage('/resourse/bg-img.png', 0, 0, 375, 600);
      ctx.drawImage('/resourse/achievement.png', 40, 40, 260, 100);
      ctx.save();
      if (this.data.rightNum<6){
        ctx.drawImage('/resourse/fail.png', 40, 150, 100, 70);
      }else if(this.data.rightNum==10){
        ctx.translate(0, 0);//设置画布上的(0,0)位置，也就是旋转的中心点
        ctx.rotate(350 * Math.PI / 180);
        ctx.drawImage('/resourse/1.png', 10, 170, 30, 70);
        ctx.drawImage('/resourse/0.png', 30, 164, 30, 70);
        ctx.drawImage('/resourse/0.png', 50, 160, 30, 70);
      }else{
          ctx.translate(0, 0);//设置画布上的(0,0)位置，也就是旋转的中心点
          ctx.rotate(350 * Math.PI / 180);
          ctx.drawImage('/resourse/' + this.data.rightNum + '.png', 0, 170, 45, 70);
          ctx.drawImage('/resourse/0.png', 30, 164, 45, 70);
      }
      ctx.restore();
      ctx.drawImage('/resourse/line.png', 40, 210, 100, 40);
      ctx.beginPath();
      ctx.font = 'normal 20px Arial, san-serif';
      ctx.setFillStyle('#fff');
      if (this.data.rightNum==6){
        ctx.drawImage('/resourse/star.png', 170, 150, 20, 20);
      } else if (this.data.rightNum == 7){
        ctx.drawImage('/resourse/star.png', 170, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 194, 150, 20, 20);
      } else if (this.data.rightNum == 8) {
        ctx.drawImage('/resourse/star.png', 170, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 194, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 218, 150, 20, 20);
      } else if (this.data.rightNum == 9) {
        ctx.drawImage('/resourse/star.png', 170, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 194, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 218, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 242, 150, 20, 20);
      } else if (this.data.rightNum == 10) {
        ctx.drawImage('/resourse/star.png', 170, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 194, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 218, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 242, 150, 20, 20);
        ctx.drawImage('/resourse/star.png', 266, 150, 20, 20);
      }
      ctx.fillText('姓名:' + this.data.username, 170, 200);
      ctx.fillText('等级:' + this.data.grade, 170, 230);
      ctx.beginPath();//开始一个新的路径  
      ctx.moveTo(40, 266);//路径的起点  
      ctx.lineTo(350, 266);//路径的终点 
      ctx.strokeStyle = '#ccc';
      ctx.stroke();//对当前路径进行描边  
      ctx.closePath();//关闭当前路径 
      ctx.fillText('评语', 40, 300);
      this.canvasTextAutoLine(ctx, this.data.comment, 40, 330, 24)
      // ctx.drawImage('/resourse/qrcode.png', 0, 382, 375, 218);
      ctx.save();
      ctx.translate(-200, 300);//设置画布上的(0,0)位置，也就是旋转的中心点
      ctx.rotate(330 * Math.PI / 180);
      ctx.arc(180, 320, 60, 0, 2 * Math.PI) //画出圆
      ctx.clip();
      // ctx.drawImage('/resourse/code.jpg',120,260,120,120);
      ctx.restore();
      ctx.draw();
      resolve();
    }).then(function () {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 750,
            height: 1200,
            destWidth: 750,
            destHeight: 1200,
            canvasId: 'firstCanvas',
            fileType: 'png',
            quality: 1,
            success: res => {
              resolve(res);
            }
          })
        }, 1000)
      })
    }).then(res => {
      return new Promise((resolve, resject) => {
        wx.hideLoading();
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: result => {
            wx.showToast({
              title: '已保存到相册',
            })
          },
          fail: res => {
            if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
              wx.showToast({
                title: '请打开相册设置',
                icon: 'none'
              })
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    console.log("再次点击图片保存到相册")
                  } else {
                    console.log("获取权限失败")
                  }
                }
              })
            }
          }
        })
      })
    })
  },
  //canvas里面文字换行函数：str:要绘制的字符串,initX:绘制字符串起始x坐标,initY:绘制字符串起始y坐标
  canvasTextAutoLine(ctx, str, initX, initY, lineHeight) {
    var lineWidth = 0;
    var canvasWidth = 300;
    var lastSubStrIndex = 0;
    for (let i = 0; i < str.length; i++) {
      lineWidth += ctx.measureText(str[i]).width;
      if (lineWidth > canvasWidth - initX) {//减去initX,防止边界出现的问题
        ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY);
        initY += lineHeight;
        lineWidth = 0;
        lastSubStrIndex = i;
      }
      if (i == str.length - 1) {
        ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
      }
    }
  },
  onShareAppMessage: function () {
    
    return {
      title: '快来瞅瞅！',
      path: `/pages/index/index`,
      success:function(){
       
      }
    }
  },
  toIndexPage(){
    wx.reLaunch({
      url: `/pages/index/index`
    })
  },
  toOtherMiniPro() {
    wx.navigateToMiniProgram({
      appId: 'wx011a82fedef36d59', // 要跳转的小程序的appid
      path: 'pages/index/index', // 跳转的目标页面
      extarData: {
        open: 'auth'
      },
      success(res) {
        // 打开成功  
      }
    })
  },
  showMaskFn() {
    this.setData({
      showMask: true
    })
  },
  maskGoOn() {
    this.setData({
      showMask: false
    })
  }
})
